<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Pform extends CI_Controller {
    
   	function __construct()
	{
		parent::__construct();
        $this->load->database();
        $this->load->helper('html');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->model('pform_model');
	}

	public function index()
	{
		$this->load->view('pform');
	}
    
   	public function sumbit_file()
	{
	       
        $data['fb_id'] = $this->input->post("fb_id");
        $data['fb_name'] = $this->input->post("fb_name");
        $data['fb_email'] = $this->input->post("fb_email");
        
        $fb_id = $data['fb_id'];
        $fb_name = $data['fb_name'];
        $fb_email = $data['fb_email'];
        
        //check repeat submit
        $all_fb_id = $this->pform_model->get_all_lists();
        foreach( $all_fb_id as $row){
            if ( $fb_id == $row['fb_id']){
                    $data['fb_id_included'] = '1';
                    break(1);
                } else {
                    $data['fb_id_included'] = '0';
                }
        }
        
        $this->form_validation->set_rules('eng_name', '英文姓名', 'required');
        $this->form_validation->set_rules('phone', '手機號碼', 'trim|required|regex_match[/^[0-9]+$/]'); //contains only 0-9
        $this->form_validation->set_rules('email', '電郵地址', 'trim|required|valid_email');
        $this->form_validation->set_rules('photo-title', '相片標題', 'required');
        $this->form_validation->set_rules('tac', '條款及細則', 'callback_tac');
        
        //if validation success
        if ($this->form_validation->run() == TRUE && $data['fb_id_included'] == '0')
        {
    	    $english_name = $this->input->post("eng_name");
            $phone = $this->input->post("phone");
            $email = $this->input->post("email");
            $photo_title = $this->input->post("photo-title");
            if( $this->input->post("marketing") == 'accepted'){
                $marketing = '1';
            }else{
                $marketing = '0';
            }
            //$fb_id = $this->input->post("fb_id");
            //$fb_name = $this->input->post("fb_name");
            //$fb_email = $this->input->post("fb_email");
            
            
            
        //check image upload
        $config['upload_path'] = 'webroot/uploads/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '6000';
        $config['max_width']  = '4096';
        $config['max_height']  = '3072';
        $config['remove_spaces']  = TRUE;
        
        $this->load->library('upload',$config);
        
         if ( !$this->upload->do_upload('inputfile'))
                {
                        $data['img_error'] = $this->upload->display_errors();
                        $this->load->view('pform', $data);
                }
                else
                {
                        //$data = array('upload_data' => $this->upload->data());
                        $img_data = $this->upload->data();
                        $img_path = 'http://www.jessicahk.com/mywayofsitting/' . 'webroot/uploads/'. $img_data['file_name'];
                        $this->pform_model->insert_list($english_name,$phone,$email,$img_path,$fb_id,$fb_name,$fb_email,$photo_title,$marketing);
                        $data['success_img'] = $img_path;
                        $data['photo_title'] = $photo_title;
                        $this->load->view('success', $data);
                }
         
         }else{
            //error message
            $this->data['eng_name'] = validation_errors();
            $this->data['phone'] = validation_errors();
            $this->data['email'] = validation_errors();
            $this->data['photo-title'] = validation_errors();
            $this->data['tac'] = validation_errors();
            $this->data['marketing'] = validation_errors();
            
            $this->load->view('pform', $data);
         }         
	}
    
    
    public function accessToken($id, $name, $email)
    {
        $data['fb_id'] = $id;
        $data['fb_name'] = str_replace("%20", " ", $name);
        $data['fb_email'] = str_replace("-", "@", $email);
        
        $this->load->view('pform',$data);
   	} 
    
    function tac()
    {
            if ($this->input->post("tac") == "tac-accepted"){
                $flag = '1';
            }else{
                $flag = '0';
            }
            
            if( $flag == "1"){
                return true;
            } else {
                $this->form_validation->set_message('tac', '請先細閱活動條款及細則');
                return false;
            }
    }
}